
int generateMazeCount = 0;
void generateMaze(){
 //画图
 for(int i = 0; i < 3; ++ i){
     generateMazeCount ++;

    for(Cell c : grid){//类似c++自动遍历
        c.show(back_ground_c);//以什么颜色展示
        //color 一个参数灰度，两个参数灰度+透明度，三个rgb，4个rgb+透明度
        
    }
    
  
    current.visited = true;
    
    
    
    Cell next = current.checkNeighbours();//找到未被访问的邻居，这一步随机就能保证地图随机
    if(next != null){
        next.visited = true;
        stack.add(current);//加入栈
        removeWalls(current,next);//清除与邻居之间的墙
        current.step = generateMazeCount;
        current = next;
    }else if (stack.size() > 0){//邻居都被访问过，走不通了返回上一个格子，再找方向
        current = stack.remove(stack.size() - 1);//应该是返回走后一个元素并删除
       // print("123");
    }
    background(20);   
    for(Cell c : grid){
      if(c.visited){
          int A = 250;
          int gap =  generateMazeCount - c.step;
          A = (int)(250 / ((gap + 1) * 0.02));
          if (A < 45) A = 0; 
          c.show(color(#ff0000,A));
      }else{
        c.show(back_ground_c);
      }
    }
    //这样能保证每个格子都被访问，不存在与外界不连通的块
  //  delay(100);

  //迷宫生成完毕，重设visited
    if(frameCount > 10 && stack.size() == 0){//framecount是已经绘制了多少帧
        drawFinish = true;

        for(Cell c : grid){
            c.addNeighbours();
            c.visited = false;
        }
        start.visited = true;
        return;
        

        
    } 
 }
}

void randomGenerate() {
   // for(int t = 0; t < 10; ++ t){
        removeWalls(grid[0], grid[1]);
        removeWalls(grid[grid.length - 1], grid[grid.length - 2]);
        ArrayList<Cell> around = new ArrayList<Cell>();
        
        for (int n = 0; n < 3; n++) {
            around.clear();
            Cell current = grid[frameCount * 3 - 3 + n];
            int i = current.i, j = current.j;
            
            int top = current.index(i, j - 1);
            int right = current.index(i + 1, j);
            int bottom = current.index(i, j + 1);
            int left = current.index(i - 1, j);
            if (top != -1) around.add(grid[top]);
            if (right != -1) around.add(grid[right]);
            if (bottom != -1) around.add(grid[bottom]);
            if (left != -1) around.add(grid[left]);
            for (Cell neighbour : around) {
                float r = random(1);
                if (r < 0.5) removeWalls(current, neighbour);
            }
        }
    
        for (Cell c : grid) c.show(color(20, 100));
    
        if (frameCount > grid.length / 3 - 1) {
            drawFinish = true;
            for (Cell c : grid) {
                c.addNeighbours();
                c.visited = false;
            }
            start.visited = true;
        }
  //  }
}


static int p[] = new int[N];
int find(int x) {
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}

void djsetGenerate() {
    removeWalls(grid[0], grid[1]);
    removeWalls(grid[grid.length - 1], grid[grid.length - 2]);

    int ver,dire;
    for(int n = 0; n < 5; ++ n)
    if(find(0) != find(grid.length - 1) ){
         // println(find(0) + "  -  " + find(grid.length - 1));
          ver =(int) random(0,rows * cols );
          ArrayList<Cell> around = new ArrayList<Cell>();
          Cell cur = grid[ver];
          int i = cur.i, j = cur.j;
          int top = cur.index(i, j - 1);
          int right = cur.index(i + 1, j);
          int bottom = cur.index(i, j + 1);
          int left = cur.index(i - 1, j);
          if (top != -1) around.add(grid[top]);
          if (right != -1) around.add(grid[right]);
          if (bottom != -1) around.add(grid[bottom]);
          if (left != -1) around.add(grid[left]);
          dire = (int)random(0,around.size());
          
          int ver1 = around.get(dire).i + around.get(dire).j *cols;
          if(find(ver) != find(ver1)){
              removeWalls(grid[ver], grid[ver1]);
     
               p[find(ver)] = find(ver1);
          }  
          println(ver + "   " + ver1);
          for (Cell c : grid) c.show(color(20, 100));
    }else{
      println("sucksuck\n");
      for (Cell c : grid) c.show(color(20, 100));
     
        drawFinish = true;
        for(Cell c : grid){
            c.addNeighbours();
            c.visited = false;
        }
        start.visited = true;
        Arrays.fill(p, 0);
        //Arrays.fill(size, 0);//填充0x3F3F3F3F
        return;
          
      }
}
